package com.ruoyi.api.worker;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.ruoyi.system.utils.TokenUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.json.JSONObject;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;


@RestController
@RequestMapping("/api/worker/tixian")
@Api(tags = "社工端提现,账户流水")
public class ApiWorkerTiXianController {

    @Autowired
    private IJiajiaWorkerService workerService;

    @Autowired
    private IJiajiaWorkerTixianService tixianService;

    @Autowired
    private IJiajiaMessageService messageService;

    @Autowired
    private IJiajiaWorkerMoneyChangeService moneyChangeService;

    @Autowired
    private IJiajiaWorkerBankCardService bankCardService;

    @Autowired
    private IJiajiaWorkerMoneyChangeService workerMoneyChangeService;


    @ApiOperation("我要提现")
    @PostMapping("/tiXian")
    public AjaxResult tiXian(@RequestParam Float money)
    {
        JiajiaWorker worker = workerService.selectJiajiaWorkerById(TokenUtil.getUid());
        if (worker == null)return AjaxResult.notLogin();

        if (money.floatValue() <= 0)return AjaxResult.error("提现金额不能小于零");
        if (money.floatValue() > worker.getMoney().floatValue() )return AjaxResult.error("提现金额不能大于您的余额");

        JiajiaWorkerBankCard bankCard = new JiajiaWorkerBankCard();
        bankCard.setWid(worker.getId());
        List<JiajiaWorkerBankCard> bankCardList = bankCardService.selectJiajiaWorkerBankCardList(bankCard);
        if (bankCardList.isEmpty())return AjaxResult.error("请先添加银行卡");
        bankCard = bankCardList.get(0);

        long count = PageHelper.count(new ISelect() {
            @Override
            public void doSelect() {
                JiajiaWorkerTixian tixian = new JiajiaWorkerTixian();
                tixian.setWid(worker.getId());
                tixian.setStatus(0);
                tixianService.selectJiajiaWorkerTixianList(tixian);
            }
        });
        if (count > 0)return AjaxResult.error("当前有待审核的提现申请，请审核成功后再操作");

        JiajiaWorkerTixian tixian = new JiajiaWorkerTixian();
        tixian.setMoney(new BigDecimal(money));
        tixian.setWid(worker.getId());
        tixian.setBankInfo(JSON.toJSONString(bankCard));
        tixianService.insertJiajiaWorkerTixian(tixian);

        JiajiaMessage message = new JiajiaMessage();
        message.setType(1);
        message.setUid(worker.getId());
        message.setTitle("申请提现");
        message.setContent("您的提现申请已提交，申请提现金额:"+money.floatValue()+"元，请等待后台审核");
        messageService.insertJiajiaMessage(message);

        //新增社工余额变动记录
        JiajiaWorkerMoneyChange workerMoneyChange=new JiajiaWorkerMoneyChange();
        workerMoneyChange.setWid(tixian.getWid());
        workerMoneyChange.setChangemoney(new BigDecimal( - tixian.getMoney().floatValue()));
        workerMoneyChange.setChangeAfter(new BigDecimal(worker.getMoney().floatValue() - tixian.getMoney().floatValue()));
        workerMoneyChange.setChangeBefor(worker.getMoney());
        workerMoneyChange.setType("申请提现成功");
        workerMoneyChange.setDes("提现金额："+tixian.getMoney().setScale(2,RoundingMode.HALF_UP)+"元,已冻结");
        workerMoneyChangeService.insertJiajiaWorkerMoneyChange(workerMoneyChange);

        //社工余额修改
        worker.setMoney(workerMoneyChange.getChangeAfter());
        workerService.updateJiajiaWorker(worker);

        return AjaxResult.success("提交成功");
    }

    @ApiOperation("账户流水")
    @PostMapping("/moneyChangeList")
    public AjaxResult moneyChangeList(@RequestParam Integer pageNo,
                                      @RequestParam Integer pageSize){

        JiajiaWorkerMoneyChange workerMoneyChange = new JiajiaWorkerMoneyChange();
        workerMoneyChange.setWid(TokenUtil.getUid());
        PageHelper.startPage(pageNo,pageSize,"addtime desc");
        List<JiajiaWorkerMoneyChange> changeList =  moneyChangeService.selectJiajiaWorkerMoneyChangeList(workerMoneyChange);

        return AjaxResult.success(changeList);
    }

    @ApiOperation("获取余额")
    @PostMapping("/getMyMoney")
    public AjaxResult getMyMoney(){

        JiajiaWorker worker = workerService.selectJiajiaWorkerById(TokenUtil.getUid());
        if (worker == null)return AjaxResult. notLogin();

        JSONObject resObj = new JSONObject();
        resObj.put("money",worker.getMoney());
        resObj.put("bank","");

        //查询正在提现的余额
        JiajiaWorkerTixian tixian = new JiajiaWorkerTixian();
        tixian.setWid(worker.getId());
        tixian.setStatus(0);
        List<JiajiaWorkerTixian>  tixianList = tixianService.selectJiajiaWorkerTixianList(tixian);

        float money = 0;
        for (JiajiaWorkerTixian model : tixianList) {
            money += model.getMoney().floatValue();
        }

        resObj.put("tiXianMoney","您有"+new BigDecimal(money).setScale(2, RoundingMode.HALF_UP)+"元提现金额正在审核！");

        if (tixianList.isEmpty()){
            resObj.put("tiXianMoney","");
        }

        JiajiaWorkerBankCard bankCard = new JiajiaWorkerBankCard();
        bankCard.setWid(worker.getId());
        List<JiajiaWorkerBankCard> bankCardList = bankCardService.selectJiajiaWorkerBankCardList(bankCard);
        if (!bankCardList.isEmpty()){
            resObj.put("bank",bankCardList.get(0).getBanrName());
        }


        return AjaxResult.success(resObj);
    }


}
